# -*- coding: utf-8 -*-

from configuration import ConfigurationManager
from lib.pipuii18n import _

import sys

if 'linux' in sys.platform:
    from Tkinter import *
elif 'win' in sys.platform:
    from tkinter import *

import tkMessageBox

cmanager = ConfigurationManager()

class ProxyManager:
    
    master = None
    
    def __init__(self, root):
        self.master = Toplevel()
        self.master.transient(root)
        self.master.title(_(u"Proxy settings"))
        self.setupWidgets()
        self.fillProxySettings()
        self.bindEvents()
    
    def fillProxySettings(self):
        self.txt_host.insert(0, cmanager.configuration.proxy_host)
        self.txt_port.insert(0, str(cmanager.configuration.proxy_port))
        self.txt_login.insert(0, cmanager.configuration.proxy_login)
        self.txt_pass.insert(0, cmanager.configuration.proxy_pass)
    
    def saveProxySettings(self):
        cmanager.save()
    
    def bindEvents(self):
        pass
        
    def setupWidgets(self):
        
        # Setting up main frame for holding widgets
        frame = Frame(self.master, width = 100)
        self.master.rowconfigure(0, weight = 1)
        self.master.columnconfigure(0, weight = 1)
        frame.grid(sticky = N+E+S+W, padx=10, pady=10)
        
        frame.columnconfigure(0, weight = 2, pad = 40)
        frame.columnconfigure(1, weight = 1, pad = 40)
        for i in xrange(5):
            frame.rowconfigure(i, weight = 1)
        
        # Setting up proxy manager window labels
        lbl_host = Label(frame, text = _(u'Host'))
        lbl_host.grid(row = 0, column = 0, sticky = N+E+W+S, padx = 5, pady = 5)

        lbl_port = Label(frame, text = _(u'Port'))
        lbl_port.grid(row = 1, column = 0, sticky = N+E+W+S, padx = 5, pady = 5)

        lbl_login = Label(frame, text = _(u'Login'))
        lbl_login.grid(row = 2, column = 0, sticky = N+E+W+S, padx = 5, pady = 5)

        lbl_pass = Label(frame, text = _(u'Password'))
        lbl_pass.grid(row = 3, column = 0, sticky = N+E+W+S, padx = 5, pady = 5)

        # Setting up proxy manager window entries
        self.txt_host = Entry(frame)
        self.txt_host.grid(row = 0, column = 1, sticky = N+E+W+S, padx = 5, pady = 5)

        self.txt_port = Entry(frame)
        self.txt_port.grid(row = 1, column = 1, sticky = N+E+W+S, padx = 5, pady = 5)

        self.txt_login = Entry(frame)
        self.txt_login.grid(row = 2, column = 1, sticky = N+E+W+S, padx = 5, pady = 5)

        self.txt_pass = Entry(frame)
        self.txt_pass.grid(row = 3, column = 1, sticky = N+E+W+S, padx = 5, pady = 5)

        # Setting up command buttons
        self.btn_OK = Button(frame, text = _(u"OK"), command = self.commandOK)
        self.btn_OK.grid(row = 4, column = 0, sticky = N+E+W+S, padx = 5, pady = 5)
        
        self.btn_cancel = Button(frame, text = _(u"Cancel"), command = self.commandCancel)
        self.btn_cancel.grid(row = 4, column = 1, sticky = N+E+W+S, padx = 5, pady = 5)
        
    def commandCancel(self):
        self.master.withdraw()
    
    def commandOK(self):
        self.saveProxySettings()
        tkMessageBox.showinfo(_(u"Success"), _(u"Preferences successfully set. Changes are made after application restart."))
        self.master.withdraw()
